import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.HashMap;
import java.util.Calendar;
import java.util.TreeMap;

public class MyCalendar{
	/**
	 * @param args
	 * @throws ParseException 
	 */
	private static Set<Excercise> metabolicSet = new HashSet<Excercise>();
	private static 	Set<Excercise> gymnasticSet = new HashSet<Excercise>();
	private static 	Set<Excercise> weightliftingSet = new HashSet<Excercise>();
	public static int days, stDay, calculatedDays;
	public static int workdays;
	public static String  startingDay;
	public static boolean wantChoose;
	public static int restdays ;
	public static Integer[] listDays;
	public static String schemeType;
	private static List<String> names = new ArrayList<String>();
	public static Scheme userScheme;
	public static void main(String[] args)throws IOException, ParseException{
		// TODO Auto-generated method stub
		Training list = new Training();
		createSets();
		menu();
		calculatedDays = days;	
		if( days%7==4 ) days++;
		else if ( days%7>=5 ) days+=2;
		days += (days/7)*2;
		List<List> listOfExcercises=list.generatePlan(metabolicSet, gymnasticSet,weightliftingSet,days);
		if( userScheme.getType() != SchemeType.FIVETWO ){
			 userScheme.crop(listOfExcercises);
		} 			
		if( userScheme.getType() != SchemeType.CUSTOM ){
			 listOfExcercises =userScheme.addRestDays(listOfExcercises,calculatedDays);
		}else{
			listOfExcercises=userScheme.addRestDays(listOfExcercises,stDay,calculatedDays,listDays);
		}
		System.out.println(listOfExcercises.size());
		setExcercises(listOfExcercises);
	}
	private static void createSets() throws IOException, ParseException{
		BufferedReader read = null;
		String line = null;
		System.out.println();
		try{
				read = new BufferedReader(new FileReader("Excercises.txt"));
				while((line = read.readLine()) != null){
					String[] nameAndType = line.split(":");
					String name = nameAndType[0].trim();
					String stype = nameAndType[1].trim();
					Excercise task = new Excercise(name,ExceriseType.valueOf(stype));
					switch (task.getType()) {
						case GYMNASTIC: 
							if(gymnasticSet.size()==5)break;
							gymnasticSet.add(task);
							break;
						case METABOLIC:
							if(metabolicSet.size()==5)break;
							metabolicSet.add(task);
							break;
						case WEIGHTLIFTING:
							if(weightliftingSet.size()==5)break;
							weightliftingSet.add(task);
							break;
						default: System.out.println("The type is incorrect!");
							break;
					}
				}
			}catch (IOException exc){
				exc.printStackTrace();
			}finally{
				read.close();
			}	
	}
	private static void menu(){
		Scanner reader = new Scanner(System.in);
		System.out.println("Pass the number of days how long you want to practise:");
		days = reader.nextInt();
		reader.nextLine();	
		System.out.println("Wchich type of Scheme You choose : (THREEONE,FIVETWO,CUSTOM)");
		schemeType = reader.nextLine();
		userScheme = new Scheme(SchemeType.valueOf(schemeType.toUpperCase()));
		if(userScheme.getType() == SchemeType.CUSTOM){
			System.out.println("Pass the names Day of week, which you want choose as rest Days: ");
			while(reader.hasNextLine()){
				String s = reader.nextLine();
				if(s.isEmpty())break;
				names.add(s);
			}
			Object[] newnames = names.toArray();
			String[] nnames = new String[newnames.length];
			for(int indd =0 ; indd<newnames.length;indd++){
				nnames[indd]=names.get(indd);
			}
			listDays=  userScheme.findRestDays(nnames);
			restdays = listDays.length;
		}
		else if(userScheme.getType() == SchemeType.THREEONE)restdays=1;
		else restdays=2;
		System.out.println("Pass the names Day of week, from what you want start training: ");
		startingDay = reader.nextLine();
	    stDay = userScheme.findStartDay(startingDay);
		reader.close();
	}
	public static void printExcercises(List<List> excercises){
			int d=1;
		for(List<Excercise> l: excercises){
				System.out.println("\n\tDAY:"+ d++);
				for(Excercise e: l){
					e.printTask();
				}
			}
	}
	public static Map setExcercises(List<List> excercises){
		HashMap<Date,List<Excercise>>  myCalendar = new HashMap();
		SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd-E" );
		Calendar cal = Calendar.getInstance();
		stDay = userScheme.findStartDay(startingDay);
		while(stDay!=cal.getTime().getDay()){
			cal.add( Calendar.DATE, 1 );
		}
		for(List<Excercise> l: excercises){
			myCalendar.put(cal.getTime(),l);
			cal.add( Calendar.DATE, 1 );
		}
		Map<Date,List<Excercise>> map = myCalendar;
		for(Map.Entry<Date,List<Excercise>> entry : map.entrySet()){
			System.out.print(dateFormat.format(entry.getKey()));
			List<Excercise> list = (List<Excercise>)(entry.getValue());
			System.out.println();
			printExc(list);
			System.out.println();
		}
		return map;
	}
	public static void printExc(List<Excercise> excercises){
		for(Excercise l: excercises){
				l.printTask();
		}
	}
}
